﻿<div ng-controller="DepotC">
    <div class="bg-light lter b-b wrapper-md">
        <h1 class="m-n font-thin h3">{{suppliername}}产品列表</h1>
    </div>
    <div class="wrapper-md" style="text-align: right;">
        <div class="panel panel-default">
            <div class="panel-heading">
                <button class="btn btn-sm btn-default" ng-click="mdlShow(true)" data-toggle="modal" data-target="#dlg">
                    <i class="fa fa-plus text"></i>
                    <span class="text">添加</span>
                </button>
                <button type="button" class="btn btn-default" ng-click="$state.go('app.Supplier_List')">返回</button>
            </div>
            <div class="table-responsive text-center">
                <table id="example1" class="table table-striped b-t b-light">
                    <thead>
                    <tr>
                        <th style="text-align: center;">名称</th>
                        <th style="text-align: center;">材质</th>
                        <th style="text-align: center;">规格</th>
                        <th style="text-align: center;">件重</th>
                        <th style="text-align: center;">操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr ng-repeat="item in pageData.Data">
                        <td style="width: 15%">{{item.Name}}</td>
                        <td style="width: 15%">{{item.Standard}}</td>
                        <td style="width: 15%">{{item.Format}}</td>
                        <td style="width: 15%">{{item.Weight}}</td>


                        <td style="width: 20%">
                            <button class="btn btn-sm btn-default" ng-click="mdlShow(false,item)" data-toggle="modal" data-target="#dlg">
                                <i class="fa fa-edit text"></i>
                                <span class="text">编辑</span>
                            </button>

                        </td>

                    </tr>
                    </tbody>
                </table>
                <div class="panel-heading" ng-include="'/htm/tpl/Supplier/Paging.html'">

                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="dlg" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button ng-click="cancel()" type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="exampleModalLabel">产品管理</h4>
                </div>
                <form name="form" class="form-validation">
                    <div class="modal-body">
                        <div class="form-group">
                            <label>请选择产品类型</label>
                            <select required id="Provider" class="form-control cangku" ng-model="NowData.Name" ng-options="value for value in CommodityNameData"></select>
                        </div>
                        <div class="form-group">
                            <label class="control-label">材质</label>
                            <input required type="text" class="form-control" ng-model="NowData.Standard">

                        </div>
                        <div class="form-group">
                            <label class="control-label">规格</label>
                            <input required type="text" class="form-control" ng-model="NowData.Format">
                        </div>
                        <div class="form-group">
                            <label class="control-label">件重</label>
                            <input required type="number" min="0" class="form-control" ng-model="NowData.Weight">
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button ng-disabled="form.$invalid" type="button" class="btn btn-default" data-dismiss="modal" ng-hide="NowData.isNew" ng-click="edit()">修改</button>
                        <button ng-disabled="form.$invalid" type="button" class="btn btn-default" data-dismiss="modal" ng-show="NowData.isNew" ng-click="add()">新建</button>
                        <button type="button" class="btn btn-primary" data-dismiss="modal" ng-click="cancel()">关闭</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

</div>
<script>
    angular.module('app') //非标优惠
        .controller("DepotC", function($scope, $state, $http, $stateParams) {
            $scope.suppliername = $stateParams.name;
//            $scope.isshow = true;
            $scope.pageData = {};
            $scope.pageData.Page = 1;

            $scope.pageData.Rows = $scope.defPageRows;
            $scope.NowData = {};

            //
            $scope.getData = function(p, r) {
                if (p) {
                    p = p < 1 ? 1 : p;
                } else p = 1;
                if (r) {
                } else r = $scope.defPageRows;
                $http({
                        url: "/api/Supplier/Commodity",
                        method: "GET",
                        params: { page: p, rows: r, supplierId: $stateParams.id }
                    })
                    .then(function(res) {
                        $scope.pageData = res.data;
                        console.log($scope.pageData);
                    }, function(error) {
                        if (error) {
                            alert("数据获取失败");
                        }
                    });
            };
            $scope.getData(1, $scope.pageData.Rows);

            $scope.mdlShow = function(isNew, data) {
//                $scope.isshow= !$scope.isshow;
                if (isNew) {
                    $scope.NowData = {};
                    $scope.NowData.isNew = true;
                } else {
                    $scope.NowData = data;
                    $scope.NowData.isNew = false;
                }
            };
            $scope.cancel = function(){
                window.location.reload() 
            };
            $scope.edit = function() {
                $http.put("/api/Supplier/EditCommodity", [$scope.NowData])
                    .then(function(res) {
                        if(res.State<=0){
                            alert(res.Msg);
                        }else {
                            alert("修改成功");
                        }
                    },function(error) {
                        if (error) {
                            alert("数据获取失败");
                        }
                    });

            };
            $scope.add = function () {
                var gid = generateUUID();
                $scope.NowData.Id = gid;
                $http.post("/api/Supplier/AddCommodity?supplierId=" + $stateParams.id, [$scope.NowData])
                    .then(function(res) {
                        if(res.State<=0){
                            alert(res.Msg);
                        }else {
                            //$scope.pageData = res.data;
                            $scope.pageData.Data.push($scope.NowData);
                            alert("添加成功");
                            // $state.go('app.Supplier_SupplierCommodity', { 'id': $stateParams.id})
                        }
                    }, function(error) {
                        if (error) {
                            alert("数据获取失败");
                        }
                    });
            }
        });
    function generateUUID() {
        var d = new Date().getTime();
        var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            var r = (d + Math.random() * 16) % 16 | 0;
            d = Math.floor(d / 16);
            return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
        });
        return uuid;
    };

</script>
